package com.seguriapp.crimeactivity.gwt.server;

import java.util.ArrayList;
import java.util.List;

import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.DispatchException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.restlet.data.MediaType;
import org.restlet.resource.ClientResource;

import com.seguriapp.common.config.GlobalConfigLoader;
import com.seguriapp.common.gwt.server.AbstractSeguriAppHandler;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListAction;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListResult;
import com.seguriapp.entities.CrimeCategory;

public class GetCrimeCategoryListHandler extends AbstractSeguriAppHandler<GetCrimeCategoryListAction, GetCrimeCategoryListResult> {

  private static final Logger LOGGER = Logger.getLogger(GetCrimeCategoryListHandler.class);

  public Class<GetCrimeCategoryListAction> getActionType() {
    return GetCrimeCategoryListAction.class;
  }

  public GetCrimeCategoryListResult execute(final GetCrimeCategoryListAction action, final ExecutionContext context) throws DispatchException {
    final GetCrimeCategoryListResult result = new GetCrimeCategoryListResult();
    try {
      final String url = GlobalConfigLoader.getInstance().getGlobalConfig().getBackEndUrl().concat(GlobalConfigLoader.getInstance().getGlobalConfig().getGetCrimeCategoryList());
      final ClientResource resource = new ClientResource(url);
      final String crimeCategoryListJs = resource.get(MediaType.TEXT_PLAIN).getText();
      LOGGER.info("Data received : \n" + crimeCategoryListJs);
      result.setCrimeCategoryList(getJsonDataFromString(crimeCategoryListJs));
    } catch (Exception e) {
      LOGGER.error("Error getting the crime category list", e);
    }
    return result;
  }

  private List<CrimeCategory> getJsonDataFromString(final String crimeCategoryListJs) {
    final List<CrimeCategory> crimeCategoryList = new ArrayList<CrimeCategory>();
    final JSONObject root = JSONObject.fromObject(crimeCategoryListJs);
    JSONArray dataArray = null;
    JSONObject jo = null;
    if (root.has("crimeCategoryList")) {
      dataArray = root.getJSONArray("crimeCategoryList");
      if (dataArray != null) {
        for (int i = 0; i < dataArray.size(); i++) {
          jo = dataArray.getJSONObject(i);
          final CrimeCategory crimeCategory = processjSONObject(jo);
          if (crimeCategory != null) {
            crimeCategoryList.add(crimeCategory);
          }
        }
      }
    }
    return crimeCategoryList;
  }

  private CrimeCategory processjSONObject(final JSONObject jo) {
    if (jo.has("id") && jo.has("name") && jo.has("description")) {
      final CrimeCategory crimeCategory = new CrimeCategory();
      crimeCategory.setId(jo.getLong("id"));
      crimeCategory.setName(jo.getString("name"));
      crimeCategory.setDescription(jo.getString("description"));
      return crimeCategory;
    }
    return null;
  }

}
